package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Question;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.QuestionService;
import cn.ztydata.teachingsystem.heaven.web.dto.questionAnswer.QuestionDto;
import cn.ztydata.teachingsystem.heaven.web.dto.questionAnswer.QuestionInfoDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.MessageFormat;

/**
 * 提问控制器类
 *
 * @author lyj
 * @since 2015-3-19
 */
@Controller
@RequestMapping("/ask")
public class QuestionController extends BaseController {
    @Autowired
    private QuestionService questionService;
    @Value("${session.key}")
    private String sessionKey;


    /**
     * 新增问题
     *
     * @param questionInfoDto 问题实体
     * @return JsonResult
     * @author lyj
     * @since 2015-3-19
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addQuestion(@Valid @RequestBody QuestionInfoDto questionInfoDto) {
        Assert.notNull(questionInfoDto.getGroupId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.notNull(questionInfoDto.getTitle(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "标题"));
        Assert.notNull(questionInfoDto.getContent(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题内容"));

        questionService.addQuestion(mapper.map(questionInfoDto, Question.class));

        log.debug("新增问题");
        return new JsonResult(true, "新增问题成功！");
    }


    /**
     * 编辑问题
     *
     * @param questionInfoDto 问题实体
     * @return JsonResult
     * @author lyj
     * @since 2015-3-19
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult editQuestion(@Valid @RequestBody QuestionInfoDto questionInfoDto) {
        Assert.notNull(questionInfoDto.getGroupId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.notNull(questionInfoDto.getTitle(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "标题"));
        Assert.notNull(questionInfoDto.getContent(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题内容"));
        Assert.notNull(questionInfoDto.getId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题ID"));

        questionService.editQuestion(mapper.map(questionInfoDto, Question.class));

        log.debug("编辑问题");
        return new JsonResult(true, "编辑问题成功！");
    }


    /**
     * 设置精选
     *
     * @param questionInfoDto

     * @return JsonResult
     * @author lyj
     * @since 2015-3-19
     */
    @RequestMapping(value = "/best", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult setChoiceness(@Valid @RequestBody QuestionInfoDto questionInfoDto) {
        Assert.notNull(questionInfoDto.getId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题ID"));
        Assert.notNull(questionInfoDto.getBest(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "精选状态"));

        questionService.setChoiceness(mapper.map(questionInfoDto, Question.class));

        log.debug("设置精选");
        return new JsonResult(true, "设置精选成功！");
    }


    /**
     * 删除问题
     *
     * @param id 问题ID
     * @return JsonResult
     * @author lyj
     * @since 2015-3-19
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteQuestion(@PathVariable long id) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题ID"));

        questionService.deleteQuestion(id);

        log.debug("删除问题");
        return new JsonResult(true, "删除问题成功");
    }


    /**
     * 获取问题明细
     *
     * @param id 问题ID
     * @return JsonResultData
     * @author lyj
     * @since 2015-3-19
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData getDetail(@PathVariable("id") long id,
                                    @RequestParam("groupId") long groupId,
                                    HttpServletRequest request){
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "问题ID"));
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));


        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        log.debug("获取问题明细");
        return new JsonResultData(mapper.map(questionService.getDetail(id, userInfo.getNumber(), groupId), QuestionDto.class));
    }

    /**
     * 问题列表
     *
     * @param groupId 群组ID
     * @param tag 标记(0全部1精选2已解决3待解决4我的)
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @return JsonResultPage
     * @author lyj
     * @since 2015-3-19
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getList(@RequestParam("groupId") long groupId,
                                  @RequestParam("tag") int tag,
                                  @RequestParam("page") int pageNum,
                                  @RequestParam("pageSize") int pageSize,
                                  HttpServletRequest request){

        Assert.isTrue(tag >= 0 && tag < 5, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "tag标记"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        Page<Question> page = questionService.getList(groupId, tag, pageNum, pageSize, userInfo);

        log.debug("问题列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), QuestionDto.class)));
    }

}
